package com.weipai.springboot.module.controller;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Advertising;
import com.weipai.springboot.model.Brand;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.AdvertisingService;
import com.weipai.springboot.module.service.FileUploadService;
import com.weipai.springboot.module.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 曹朋飞
 * @Date: 2019/01/09 09:48
 * 
 * 广告管理控制类
 */
@RestController
public class AdvertisingController {
    
    @Autowired
    private AdvertisingService advertisingService;
    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/advertising/list", method = RequestMethod.GET)
    public Response list(@RequestParam(value = "page") Integer page,
                         @RequestParam(value = "size") Integer size,
                         @RequestParam(value = "draw", required = false) String draw,
                         @RequestParam(value = "type", required = false) String type){
        return advertisingService.list(page, size, draw, type);
    }


    /**
     * 广告图片的保存/修改
     * @param advertising
     * @return
     */
    @RequestMapping(value = "/advertising/save", method = RequestMethod.POST)
    public Response save(Advertising advertising){
        return advertisingService.save(advertising);
    }

    /**
     * 图片上传
     * @param request
     * @return
     */
    @PostMapping("/advertising/img")
    public Response save(HttpServletRequest request, MultipartFile img){
        Response response = new Response();
        if(img!=null){
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if(result.equals("error")){
                response.setCode(RetCode.IMG_ERROR);
            }else if(result.equals("存储失败")){
                response.setCode(RetCode.REQUEST_ERROR);
            }else{
                response.setCode(RetCode.SUCCESS);
                response.setData(result);
            }
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    /**
     * 更改状态
     * @param id
     * @return
     */
    @RequestMapping(value = "/advertising/changeStatus", method = RequestMethod.GET)
    public Response changeStatus(String id){
        return advertisingService.changeStatus(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/advertising/delete", method = RequestMethod.GET)
    public Response delete(String id){
        return advertisingService.delete(id);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
